package com.meiqiongproperty.yiguanjia.fragment.user;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.ForgotPasswordLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendForgotPassword;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendMsg;
import com.meiqiongproperty.yiguanjia.httpbean.user.ForGetpassword;
import com.meiqiongproperty.yiguanjia.httpbean.user.SmSCode;
import com.meiqiongproperty.yiguanjia.utils.CountDownTimerUtils;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.user
 * @User ChenJing
 * @Date 2017/4/25
 */

public class ForgotPasswordFragment extends BaseFragment{
    ForgotPasswordLayoutBinding binding;
    private String phone,code,passowrd;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.forgot_password_layout,container,false);
        binding.setClick(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.imgTopBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
    }

    public void getCode_Click(View view){
        if (isClikVualuePhone()){
            sendMsg();
        }
    }
    private void sendMsg() {
        //type 登录：LOGIN 注册：REGISTER 找回密码：RESETPASSWORD
        SendMsg sendmsg=new SendMsg();
        sendmsg.setMobile(phone);
        sendmsg.setType("RESETPASSWORD");
        ApiService.sendMsg(getActivity(), new SimpleHttpCallback<SmSCode>() {
            @Override
            public void onNext(SmSCode smsCode) {
                LogUtil.d("smsCode="+smsCode.getCode());
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(binding.getVerifyCodeBtn, 60000, 1000);
                mCountDownTimerUtils.start();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, Utils.getRequestBody(sendmsg));
    }
    public boolean isClikVualuePhone() {
        phone = binding.inputPhone.getEditableText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    public void commitClick(View view){
        if (isCheckValue()){
            resetPassword();
        }
    }

    //发送注册请求
    private void resetPassword() {
        SendForgotPassword sennforgetpassowrd=new SendForgotPassword();
        sennforgetpassowrd.setValidCode(code);
        sennforgetpassowrd.setPassword(passowrd);
        sennforgetpassowrd.setMobile(phone);
        ApiService.resetPassword(getActivity(), new SimpleHttpCallback<ForGetpassword>() {
            @Override
            public void onNext(ForGetpassword forGetpassword) {
                Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_LONG).show();
                getActivity().finish();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, Utils.getRequestBody(sennforgetpassowrd));
    }

    public boolean isCheckValue() {
        if (TextUtils.isEmpty(phone)){
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_LONG).show();
            return false;
        }
        code=binding.verifyCode.getEditableText().toString();
        if (TextUtils.isEmpty(code)){
            Toast.makeText(getActivity(), "请输入验证码", Toast.LENGTH_LONG).show();
            return false;
        }
        passowrd=binding.password.getEditableText().toString();
        if (TextUtils.isEmpty(passowrd)){
            Toast.makeText(getActivity(),"请输入密码",Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }
}
